import sys
import traceback
from tkinter import messagebox

secret_card_counter = {}
process_first = False

info = open('secret_card_info', 'w', encoding='utf8')
# for i in range(0, 200000, 100000):
for i in range(0, 1664200000, 100000):
    # for i in range(1, 2, 100000):
    file = 'D:/PycharmProjects/PyTorchTest/read_test/202205part2.csv_results/202205part2.csv-{}-{}.CSV'.format(i, i + 100001)
    # file = 'D:/PycharmProjects/PyTorchTest/read_test/202205part2.csv_results/202205part2.csv-1-100000.CSV'
    with open(file, 'r',encoding='utf8') as f:
        try:
            for line in f:
                if not process_first:
                    process_first = True
                    continue
                if line == '"\n':
                    continue

                line = line.replace('"', '').replace('\n', '').replace("'", "")
                line_split = line.split(',')

                if len(line_split) < 8:
                    info.write(line + '\n')
                secret_card = line_split[7]
                if secret_card not in secret_card_counter:
                    secret_card_counter[secret_card] = 0
                secret_card_counter[secret_card] += 1
        except Exception as e:
            messagebox.showinfo("提示", "发生错误")
            traceback.print_exc()
            sys.exit(-1)

    print('{} finish'.format(file))
info.close()

print('finish read, start write')
with open('../secret_card_counter', 'w', encoding='utf8') as f:
    for secret_card, count in secret_card_counter.items():
        line = '{}={}\n'.format(secret_card, count)
        f.write(line)
print('finish write')